﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Jzy.Model.DocManger;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Attach;
using Frame.Lib.Core;

namespace Jzy.DAL.DocManger
{
    public class DocumentDAL:BaseDAL
    {
        public DocumentDAL()            
        {}

        public DataTable GetList(int dirID)
        {
            string strSql = "select a.* ,b.[ID] as UserID from " + OTableName.Document + " a left join " + STableName.User + " b on a.CreatorID = b.ID where DirID=" + dirID;
            DataTable dt = dbOper.Query(strSql);
            return dt;
        }

        public int GetID(string strName, int dirID)
        {
            string strSql = "select ID from " + OTableName.Document + " where DirID=" + dirID + " and [Name]='" + strName + "'";
            int id = dbOper.GetScalar<int>(strSql);
            return id;
        }

        public DataTable Search(string strName)
        {
            string strSql = "select *,(select count(*) from " + OTableName.DocAttach + " where BindID=a.ID) as AttachCount from " + OTableName.Document + " a where [Name] like '%" + strName + "%'";
            DataTable dt = dbOper.Query(strSql);
            return dt;
        }

        public bool Exist(int dirID, string strName, string strDocID)
        {
            string strSql = "select count(*) from " + OTableName.Document + " where [Name]='" + strName + "' and DirID=" + dirID;
            if (strDocID != null)
            {
                strSql += " and [ID]!=" + strDocID;
            }
            int count =  dbOper.GetScalar<int>(strSql);
            bool flag = (count == 0) ? false : true;
            return flag;
        }

        public Dictionary<int, int> GetCreatorIDs(List<int> ids)
        {
            string strIDs = ConvertUtil.ConvertToDbStr(ids);
            string strSql = "select ID,CreatorID from " + OTableName.Document + " where ID in (" + strIDs + ")";
            DataTable dt = dbOper.Query(strSql);
            Dictionary<int, int> ditCreatorIDs = new Dictionary<int, int>();
            foreach (DataRow dr in dt.Rows)
            {
                ditCreatorIDs.Add((int)dr["ID"], (int)dr["CreatorID"]);
            }
            return ditCreatorIDs;
        }

        public void Move(List<int> lstIDs, int dirID)
        {
            string strIDs = ConvertUtil.ConvertToDbStr(lstIDs);
            string strSql = "update "+ OTableName.Document + " set DirID=" + dirID + " where ID in (" + strIDs + ")";
            dbOper.Query(strSql);
        }

        public List<int> GetIDsByAttachIDs(List<int> attachIDs)
        {
            string attachids = ConvertUtil.ConvertToDbStr(attachIDs);
            string sql = "select distinct BindID from " + OTableName.DocAttach + " where ID in (" + attachids + ")";

            List<int> ids = dbOper.Query<int>(sql);
            return ids;
        }

        public List<AttachInfo> GetAttachList(int dirID)
        {
            string sql = "select * from " + OTableName.DocAttach + " where ID in " +
                         "(select min(a.ID) from " + OTableName.DocAttach + " as a inner join " + OTableName.Document + " as b on a.BindID = b.ID where b.DirID= @p0 Group by a.BindID)";
            List<AttachInfo> list = dbOper.Query<AttachInfo>(sql, dirID);
            return list;
        }

        public IEnumerable<DocumentInfo> GetListByDir(int dirID, string name)
        {

            string sql = "select a.*,b.Name as DirName from " + OTableName.Document + " as a inner join " + OTableName.DocDir + " as b on a.DirID = b.ID where a.DirID = @p0";

            if (!string.IsNullOrEmpty(name))
            {
                sql += " and a.Name like @p1";
            }

            sql += " order by a.Orders, a.InputTime desc";

            List<DocumentInfo> docs = dbOper.Query<DocumentInfo>(sql, dirID, "%" + name + "%");
            return docs;
        }

       
    }
}
